package com.yangaobo.mapper;

import com.yangaobo.dto.ClubPageDTO;
import com.yangaobo.entity.Club;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author 杨奥博
 */
@Repository
public interface ClubMapper {
    @Insert("""
            insert into cms_club(`title`, `tag`, `fk_direction_id`, `start_time`, `expected_end_time`,`end_time` , `info`, `deleted`, `version`, `created`, `updated`)
            values (#{title}, #{tag}, #{fkDirectionId}, #{startTime}, #{expectedEndTime},#{endTime} , #{info}, #{deleted}, #{version}, #{created}, #{updated})
            """)
    int insert(Club club);

    @Results(
            id = "clubResultMap",
            value = {
                    @Result(property = "id",column = "id",id = true),
                    @Result(property = "fkDirectionId",column = "fk_direction_id"),
                    @Result(property = "direction",column = "fk_direction_id",one = @One(select = "com.yangaobo.mapper.DirectionMapper.select")),
            }
    )
    @Select("""
            select * from cms_club t where t.id = #{param1} and t.deleted = 0
            """)
    Club select(Long id);

    @ResultMap("clubResultMap")
    @Select("""
            <script>
            select * from cms_club t
           <where>
                <if test='title != null'> title like concat('%', #{title}, '%') and </if>
                t.deleted = 0
            </where>
            </script>
            """)
    List<Club> list(ClubPageDTO dto);

    @Update("""
            <script>
            update cms_club
            <set>
                <if test="title != null">title = #{title},</if>
                <if test="tag != null">tag = #{tag},</if>
                <if test="fkDirectionId != null">fk_direction_id = #{fkDirectionId},</if>
                <if test="startTime != null">start_time = #{startTime},</if>
                <if test="expectedEndTime != null">expected_end_time = #{expectedEndTime},</if>
                <if test="endTime != null">end_time = #{endTime},</if>
                <if test="info != null">info = #{info},</if>
                <if test="deleted != null">deleted = #{deleted},</if>
                <if test="created != null">created = #{created},</if>
                <if test="updated != null">updated = #{updated},</if>
                version=version+1
            </set>
            where id = #{id} and deleted = 0 and version = #{version}
            </script>
            """
            )
    int update(Club club);

    @Update("""
            update cms_club set deleted = 1, updated = current_timestamp where id = #{id}
            """)
    int delete(Long id);

    @Update("""
            <script>
            update cms_club set deleted = 1, updated = current_timestamp where
            <foreach collection='list' item='e' open='id in (' close= ')' separator=','>
                #{e}
            </foreach>
            </script>
            """)
    int deleteBatch(List<Long> ids);

}
